<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 * socket 基础操作类
 * @name    SocketBase
 * @see  
 * @version	1.0.0 (Tue Nov 04 15:19:32 CST 2008)
 * @author	sanshi
 */
class SocketBase
{
	var $socket_host;
	var $socket_port;
	var $socket_timeout;
	var $get_timeoout;
	var $errno;
	var $errstr;
	var $socket_fp;
	function SockestBase(){

	}
	function set($socket_host,$socket_port,$socket_timeout=1,$get_timeoout=10)
	{
		$this->socket_host = $socket_host;
		$this->socket_port = $socket_port;
		$this->socket_timeout = $socket_timeout;
		$this->get_timeoout = $get_timeoout;
		return $this->connect_socket();
	}
	function connect_socket()
	{
		$this->socket_fp = fsockopen($this->socket_host,$this->socket_port,$this->errno, $this->errstr, $this->socket_timeout);
		if(!empty($this->errno))
		{
			exit();
		}
		return $this->socket_fp;
	}
	function write_socket($send_msg){
		if(is_resource($this->socket_fp))
		{
			return fwrite($this->socket_fp, $send_msg);
		}else{
			return false;
		}
	}
	function read_socket($read_length)
	{
		$temp = "";
		//计算获取长度
		$get_length = $read_length > 128 || $read_length< 0 ? 128 : $read_length;
		if(is_resource($this->socket_fp))
		{
			stream_set_timeout($this->socket_fp,1);
			$time = time();
			while (!feof($this->socket_fp))
			{
				$temp .= fgets($this->socket_fp, $get_length);
				/*
				echo strlen($temp)."<br/>";exit();
				if(time()- $time > $this->get_timeoout)
				{
					$temp = "";
					break;
				}
				*/
			}
		}
		echo strlen($temp)."<br/>";
		exit();
		return $temp;
	}
	function close_socket()
	{
		return @fclose($this->socket_fp);
	}
}
?>